<?php

namespace app\admin\controller;

use app\models\{School as Schools, Classlist, Pxsubject, Special, Lists, Userinfo, Order, Bed, Clothes, Eat, Orderdetail, Banji};
use think\Controller;
use think\facade\Env;
use think\Db;
use oss\Oss;

class Base extends Controller
{

    public function initialize()
    {
        $menu = config('menu.menu');
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());
        $path = [];
        if (isset($menu[$controller])) {
            $path['c'] = $menu[$controller]['name'] ?: null;
            $path['a'] = $menu[$controller]['sub'][$action]['name'] ?: null;
            $path['s'] = $menu[$controller]['sub'][$action]['show'] ?: null;
        }
        $this->assign('path', $path);
    }

    protected function get_rand_str($len = 6)
    {
        $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'), array('$', '@', '#', '%', '&'));
        shuffle($arr);
        $sub_arr = array_slice($arr, 0, $len);
        return implode('', $sub_arr);
    }

    public function upload_img()
    {
        $file = $_FILES['file'];
        $oss = new Oss();
        $oss->setUploadType('ram');
        $url = $oss->uploadPostFile($file, 'bmadmin/' . date('Y-m-d'));
        return json(['code' => 0,'status' => 0, 'data' => ['src' => $url]]);
    }

    // public function upload_img()
    // {
    //     $file = request()->file('file');
    //     $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'jpg,png,gif,jpeg'])
    //         ->move(ROOT_PATH . 'uploads');

    //     if (!$info) {
    //         return json(['code' => 1, 'msg' => $file->getError()]);
    //     } else {
    //         $url = '/uploads' .'/'. $info->getSaveName();
    //         return json(['code' => 0, 'data' => ['src' => $url]]);
    //     }
    // }
}
